package com.yiqi.liebang.feature.setting.view;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.common.base.Joiner;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyTextView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.PrivacyBo;
import com.yiqi.liebang.entity.bo.SelectBo;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.setting.contract.PrivacyContract;
import com.yiqi.liebang.feature.setting.dependencies.login.DaggerPrivacyComponent;
import com.yiqi.liebang.feature.setting.dependencies.login.PrivacyPresenterModule;
import com.yiqi.liebang.feature.setting.view.adapter.BlackUserAdapter;
import com.yiqi.liebang.feature.setting.view.adapter.PrivacyAdapter;
import com.yiqi.liebang.framework.DataServer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PrivacyPolicyActivity extends BaseActivity implements PrivacyContract.View, BaseQuickAdapter.OnItemChildClickListener, CompoundButton.OnCheckedChangeListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_private_data)
    RecyclerView mRvPrivateData;
    @BindView(R.id.rv_private_msg)
    RecyclerView mRvPrivateMsg;
    @BindView(R.id.rv_private_add)
    RecyclerView mRvPrivateAdd;
    @BindView(R.id.rv_private_blank)
    RecyclerView mRvPrivateBlank;
    @BindView(R.id.cbx_private_add_all)
    CheckBox mCbxPrivateAddAll;
    @BindView(R.id.cbx_private_add_effect_10)
    CheckBox mCbxPrivateAddEffect10;
    @BindView(R.id.cbx_private_add_basic)
    CheckBox mCbxPrivateAddBasic;
    @BindView(R.id.cbx_private_add_effect_position)
    CheckBox mCbxPrivateAddPosition;
    @BindView(R.id.cbx_private_add_education)
    CheckBox mCbxPrivateAddEducation;
    @BindView(R.id.rv_show_phone)
    RecyclerView mRvShowPhone;

    private PrivacyAdapter  mPhoneAdapter,mDataAdapter, mMsgAdapter;
    private BlackUserAdapter mBlackUserAdapter;

    private String userMessagePrivacyType = null;
    private String userFriendPrivacyType = null;
    private String userDataPrivacyType = null;
    private String userPhoneType = null;
    private String blackUerId = null;
    private int editType = 0;

    private int mPos;
    @Inject
    PrivacyContract.Presenter mPresenter;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_privacy_policy;
    }

    @Override
    protected void initInjector() {
        DaggerPrivacyComponent.builder().privacyPresenterModule(new PrivacyPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "隐私策略", true, true);
        initAdpter();
    }

    @Override
    protected void initData() {
        mPresenter.getPrivacyData();

    }

    private List<String> selectDatas = new ArrayList<>();

    private void initAdapter(PrivacyBo s) {
        //黑名单
        EmptyTextView emptyTextView = new EmptyTextView(mRvPrivateBlank);
        mRvPrivateBlank.setLayoutManager(new LinearLayoutManager(this));
        mRvPrivateBlank.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(4));
        mBlackUserAdapter = new BlackUserAdapter();
        mBlackUserAdapter.bindToRecyclerView(mRvPrivateBlank);
        if (s.getUser() != null && s.getUser().size() > 0) {
            mBlackUserAdapter.setNewData(s.getUser());
        } else {
            mBlackUserAdapter.setEmptyView(emptyTextView.getEmptyView("暂无黑名单好友"));
        }
        mBlackUserAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(PrivacyPolicyActivity.this, UserInfoActivity.class);
                intent.putExtra("is_view_my", false);
                intent.putExtra("visitorUid", mBlackUserAdapter.getData().get(position).getUserUid());
                startActivity(intent);
            }
        });
        mBlackUserAdapter.setOnItemChildClickListener(this);
        if (s.getPhonePrivacy() != null) {
            userPhoneType = s.getPhonePrivacy().getType();
            if (s.getPhonePrivacy().getType().equals("0")) {
                mPhoneAdapter.changeState(1);
            } else if (s.getPhonePrivacy().getType().equals("1")) {
                mPhoneAdapter.changeState(0);
            }
        }

        if (s.getUserDataPrivacy() != null) {
            userDataPrivacyType = s.getUserDataPrivacy().getType();
            if (s.getUserDataPrivacy().getType().equals("0")) {
                mDataAdapter.changeState(1);
            } else if (s.getUserDataPrivacy().getType().equals("1")) {
                mDataAdapter.changeState(0);
            }
        }


        if (s.getUserMessagePrivacy() != null) {
            userDataPrivacyType = s.getUserMessagePrivacy().getType();
            if (s.getUserMessagePrivacy().getType().equals("0")) {
                mMsgAdapter.changeState(1);
            } else if (s.getUserMessagePrivacy().getType().equals("1")) {
                mMsgAdapter.changeState(0);
            }
        }
        if (s.getUserFriendPrivacy() != null) {
            userFriendPrivacyType = s.getUserFriendPrivacy().getType();
            List<String> list = Arrays.asList(userFriendPrivacyType.split(","));
            selectDatas = new ArrayList<>(list);
            if (userFriendPrivacyType.contains("0")) {
                mCbxPrivateAddAll.setChecked(true);
            } else if (userFriendPrivacyType.contains("1")) {
                mCbxPrivateAddEffect10.setChecked(true);
            }
            if (userFriendPrivacyType.contains("2")) {
                mCbxPrivateAddBasic.setChecked(true);
            }
            if (userFriendPrivacyType.contains("3")) {
                mCbxPrivateAddPosition.setChecked(true);
            }
            if (userFriendPrivacyType.contains("4")) {
                mCbxPrivateAddEducation.setChecked(true);
            }


        }


    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mCbxPrivateAddAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    mPresenter.editPrivacy("2", "0", null);
                    mCbxPrivateAddEffect10.setChecked(false);
                    mCbxPrivateAddBasic.setChecked(false);
                    mCbxPrivateAddPosition.setChecked(false);
                    mCbxPrivateAddEducation.setChecked(false);

                }
            }
        });

        mCbxPrivateAddEffect10.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    selectDatas.add("1");
                    mCbxPrivateAddAll.setChecked(false);
                } else {
                    selectDatas.remove("1");
                }
                removeDuplicate(selectDatas);
                mPresenter.editPrivacy("2", Joiner.on(",").join(selectDatas), null);
            }
        });

        mCbxPrivateAddBasic.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    selectDatas.add("2");
                    mCbxPrivateAddAll.setChecked(false);
                } else {
                    selectDatas.remove("2");
                }
                removeDuplicate(selectDatas);
                mPresenter.editPrivacy("2", Joiner.on(",").join(selectDatas), null);
            }
        });

        mCbxPrivateAddPosition.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    selectDatas.add("3");
                    mCbxPrivateAddAll.setChecked(false);
                } else {
                    selectDatas.remove("3");
                }
                removeDuplicate(selectDatas);
                mPresenter.editPrivacy("2", Joiner.on(",").join(selectDatas), null);
            }
        });

        mCbxPrivateAddEducation.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!buttonView.isPressed()) {
                    return;
                }
                if (isChecked) {
                    selectDatas.add("4");
                    mCbxPrivateAddAll.setChecked(false);
                } else {
                    selectDatas.remove("4");
                }
                removeDuplicate(selectDatas);
                mPresenter.editPrivacy("2", Joiner.on(",").join(selectDatas), null);
            }
        });

    }

    private static void removeDuplicate(List<String> list) {
        LinkedHashSet<String> set = new LinkedHashSet<String>(list.size());
        set.addAll(list);
        list.clear();
        list.addAll(set);
    }

    @Override
    public void showPrivacySuccess(PrivacyBo s) {
        initAdapter(s);
    }

    @Override
    public void showEditSuccess() {
        T.showShort("修改成功！");
        if (mBlackUserAdapter != null && editType == 3)
            mBlackUserAdapter.remove(mPos);
    }

    @Override
    public void showError(String error) {
        T.showShort(error);

    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        editType = 3;
        mPos = position;
        mPresenter.editPrivacy("3", null, mBlackUserAdapter.getData().get(position).getUserUid());
    }


    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        SelectBo selectBo = (SelectBo) buttonView.getTag();
        if (!buttonView.isPressed()) {
            return;
        }
        if (isChecked) {
            mCbxPrivateAddAll.setChecked(false);
            selectDatas.add(selectBo.getType() + "");
        } else {
            if (selectDatas.contains(selectBo.getType() + "")) {
                selectDatas.remove(selectBo.getType() + "");
            }
        }

        String str = Joiner.on(",").join(selectDatas);
        mPresenter.editPrivacy("2", str, null);
    }

    private void initAdpter() {

        //显示手机号
        mRvShowPhone.setLayoutManager(new LinearLayoutManager(this));
        mRvShowPhone.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(4));
        mPhoneAdapter = new PrivacyAdapter(DataServer.getShowPhoneData());
        mRvShowPhone.setAdapter(mPhoneAdapter);
        mPhoneAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                editType = 1;
                mPhoneAdapter.changeState(position);
                userPhoneType = position == 0 ? 1 + "" : 0 + "";
                mPresenter.editPrivacy("4", userPhoneType, null);
            }
        });


        //查看资料
        mRvPrivateData.setLayoutManager(new LinearLayoutManager(this));
        mRvPrivateData.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(4));
        mDataAdapter = new PrivacyAdapter(DataServer.getPrivateData());
        mRvPrivateData.setAdapter(mDataAdapter);
        mDataAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mDataAdapter.changeState(position);
                userDataPrivacyType = position == 0 ? 1 + "" : 0 + "";
                mPresenter.editPrivacy("0", userDataPrivacyType, null);
            }
        });

        //发消息设置
        mRvPrivateMsg.setLayoutManager(new LinearLayoutManager(this));
        mRvPrivateMsg.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(4));
        mMsgAdapter = new PrivacyAdapter(DataServer.getPrivateData());
        mRvPrivateMsg.setAdapter(mMsgAdapter);
        mMsgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mMsgAdapter.changeState(position);
                userMessagePrivacyType = position == 0 ? 1 + "" : 0 + "";
                mPresenter.editPrivacy("1", userMessagePrivacyType, null);
            }
        });


        //添加好友设置
        mRvPrivateAdd.setLayoutManager(new LinearLayoutManager(this));
        mRvPrivateAdd.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(4));
//        mBlackAdapter = new BlackAdapter(DataServer.getAddFriendsData());
//        mRvPrivateAdd.setAdapter(mBlackAdapter);
//        mBlackAdapter.setOnCheckedChangeListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
